//创建页面
import 'package:bilibili/page/home_page.dart';
import 'package:bilibili/page/registration_page.dart';
import 'package:bilibili/page/theme_toggle_page.dart';
import 'package:bilibili/page/video_detail_page.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../page/login_page.dart';

typedef RouteChangeListener = void Function(
    RouteStatusInfo current, RouteStatusInfo? pre);

//路由信息
class RouteStatusInfo {
  final RouteStatus routeStatus;
  final Widget page;

  RouteStatusInfo(this.routeStatus, this.page);
}

MaterialPage pageWrap(Widget child, String name, {bool canPop = true}) {
  return MaterialPage(
      name: name, key: ValueKey(child.hashCode), child: child, canPop: canPop);
}

//获取routeStatus 在页面堆栈中的位置
int getPageIndex(List<MaterialPage> pages, RouteStatus routeStatus) {
  for (var i = 0; i < pages.length; i++) {
    if (getRouteStatus(pages[i]) == routeStatus) {
      return i;
    }
  }
  return -1;
}

///自定义路由的状态
enum RouteStatus { login, registration, home, detail, theme, unknown }

/// 获取对应的RouteStatus
RouteStatus getRouteStatus(MaterialPage page) {
  if (page.child is HomePage) {
    return RouteStatus.home;
  }

  if (page.child is VideoDetailPage) {
    return RouteStatus.detail;
  }

  if (page.child is LoginPage) {
    return RouteStatus.login;
  }
  if (page.child is RegistrationPage) {
    return RouteStatus.registration;
  }

  if (page.child is ThemeTogglePage) {
    return RouteStatus.theme;
  }

  return RouteStatus.unknown;
}

class HiNavigator extends _RouteJumpListener {
  static HiNavigator? _instance;

  HiNavigator._();

  RouteJumpListener? _jumpListener;
  final List<RouteChangeListener> _routeChangeListeners =
      List.empty(growable: true);
  RouteStatusInfo? _current;

  static getInstance() {
    _instance ??= HiNavigator._();
    return _instance!;
  }

  void registerJumpListener(RouteJumpListener listener) {
    _jumpListener = listener;
  }

  @override
  void onJumpTo(RouteStatus routeStatus, {Map? args}) {
    _jumpListener?.onJumpTo(routeStatus, args: args);
  }

  void addChangeListener(RouteChangeListener listener) {
    _routeChangeListeners.add(listener);
  }

  void removeChangeListener(RouteChangeListener listener) {
    _routeChangeListeners.remove(listener);
  }

  void notify(List<MaterialPage> currentPages, List<MaterialPage> prePages) {
    if (currentPages == prePages) return;
    var routeStatus = getRouteStatus(currentPages.last);
    var page = currentPages.last.child;
    var routeStatusInfo = RouteStatusInfo(routeStatus, page);
    _notify(routeStatusInfo);
  }

  void _notify(RouteStatusInfo current) {
    if (kDebugMode) {
      print("hi_navigator:current: ${current.page}");
      print("hi_navigator:pre: ${_current?.page}");
    }

    for (var listener in _routeChangeListeners) {
      listener(current, _current);
    }
    _current = current;
  }
}

///抽象类供Navigator 实现
abstract class _RouteJumpListener {
  void onJumpTo(RouteStatus routeStatus, {Map? args});
}

typedef OnJumpTo = void Function(RouteStatus routeStatus, {Map? args});

///定义路由跳转的逻辑功能
class RouteJumpListener {
  final OnJumpTo onJumpTo;

  RouteJumpListener({required this.onJumpTo});
}
